/////////////////////////////////////////////////////////////////////////////
// Name:        listctrlItem.cpp
// Purpose:     
// Author:      Herry Sugiharto
// Modified by: 
// Created:     25/07/2009 16:39:19
// RCS-ID:      
// Copyright:   Copyright (c) 2000, 2008 Krida Software Division
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "wx/imaglist.h"
////@end includes

#include "slposapp.h"
#include "listctrlItem.h"

////@begin XPM images
////@end XPM images


/*!
 * listctrlItem type definition
 */

IMPLEMENT_DYNAMIC_CLASS( listctrlItem, wxListCtrl )


/*!
 * listctrlItem event table definition
 */

BEGIN_EVENT_TABLE( listctrlItem, wxListCtrl )

////@begin listctrlItem event table entries
////@end listctrlItem event table entries

END_EVENT_TABLE()


/*!
 * listctrlItem constructors
 */
listctrlItem::listctrlItem()
{

}


listctrlItem::listctrlItem(wxWindow* parent, 
                           wxWindowID id, 
                           const wxPoint& pos, 
                           const wxSize& size, 
                           long style, 
                           const wxValidator& validator)
                           : wxListCtrl(parent, id, pos, size, style, validator)
{
    wxListItem itemCol;
    itemCol.SetText(_("ID"));
    itemCol.SetWidth(100);
    InsertColumn(0, itemCol);
    itemCol.SetText(_("Description"));
    itemCol.SetWidth(300);
    InsertColumn(1, itemCol);
    itemCol.SetText(_("Qty"));
    itemCol.SetWidth(40);
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
    InsertColumn(2, itemCol);
    itemCol.SetText(_("Unit Price"));
    itemCol.SetWidth(100);
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
    InsertColumn(3, itemCol);
    itemCol.SetText(_("Disc%"));
    itemCol.SetWidth(50);
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
    InsertColumn(4, itemCol);
    itemCol.SetText(_("Line Total"));
    itemCol.SetWidth(100);
    itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
    InsertColumn(5, itemCol);
}


/*!
 * listctrlItem destructor
 */

listctrlItem::~listctrlItem()
{
////@begin listctrlItem destruction
////@end listctrlItem destruction
}


bool listctrlItem::AddToList(wxString& ScannedItem, wxString& Quantity, wxString& Discount)
{
    // contruct the query according to user's entered item
    wxString strqry;
    strqry = wxT("SELECT partnumber, description, sellprice, id FROM parts ");
    strqry += wxT("WHERE (upper(partnumber) = '") + ScannedItem.Upper() + wxT("') ");
    
    // get rows from table
    PGresult *res;
    res = PQexec( wxGetApp().conn, strqry.char_str() );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        //LastError.Printf(_("Unable to get record from table: %s"), PQerrorMessage(conn));
        PQclear(res);
        return false;
    }
    
    // next, add the rows to listctrl
    wxString buf;
    long bufdata = 0;
    if ( PQntuples(res) == 1 )
    {
        buf.Empty();
        buf = wxT(PQgetvalue(res, 0, 0));
        InsertItem(0, buf);
        
        buf.Empty();
        buf = wxT(PQgetvalue(res, 0, 1));
        SetItem(0, 1, buf);
        
        SetItem(0, 2, Quantity);
        
        buf.Empty();
        buf = wxT(PQgetvalue(res, 0, 2));
        SetItem(0, 3, buf);
        
        SetItem(0, 4, Discount);
        
        double qty, disc, price, linetotal;
        Quantity.ToDouble(&qty);
        Discount.ToDouble(&disc); disc = disc / 100; disc = 1 - disc;
        buf.ToDouble(&price);
        linetotal = price * disc * qty;
        
        buf.Empty();
        buf << linetotal;
        SetItem(0, 5, buf);
        
        buf.Empty();
        buf = wxT(PQgetvalue(res, 0, 3));
        buf.ToLong(&bufdata);
        SetItemData(0, bufdata);

    } else {
        //wxMessageBox(_("Item not found!"));
        return false;
    };
    
    // clear the result and others to avoid memory leak
    PQclear(res);
    buf.Clear();
    strqry.Clear();
    
    return true;
}


double listctrlItem::GetGrandTotal()
{
    double linetotal;
    
    //reset the grandtotal to zero
    double GrandTotal = 0;
    
    //get the number of items from the current list
    int totalitems = GetItemCount();
    
    for (long i = 0; i <= totalitems-1; i++) {
        wxListItem item;
        item.m_mask = wxLIST_MASK_TEXT;
        item.m_itemId = i;
        item.m_col = 5;
        GetItem(item);
        
        //convert the price to double
        item.m_text.ToDouble(&linetotal);
        
        // add the amount to the grand total
        GrandTotal = GrandTotal + linetotal;
    };
    
    return GrandTotal;
};
